package com.zkhz.base.util.system;

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 国际化消息工具类。
 *
 * 如果code含空格，将被替换为.，例如 Not Found 替换为 Not.Found
 *
 * @author wuzeqin
 * @date 2020/6/14
 **/

@Component
public class I18nUtil implements MessageSourceAware {

    private static MessageSourceAccessor MESSAGES;

    private static Pattern ChinesePattern = Pattern.compile("[\u4e00-\u9fa5]");

    @Override
    public void setMessageSource(@Nonnull MessageSource messageSource) {
        MESSAGES = new MessageSourceAccessor(messageSource);
    }

    public static String getMessage(String code) throws NoSuchMessageException {
        return getMessage(code, true);
    }

    public static String getMessage(String code, boolean useCodeAsDefaultMessage) throws NoSuchMessageException {
        int i = code.indexOf(":");
        if (i > 0) {   //如code有冒号，那么只取冒号前那部分作为key
            return getMessage(code.substring(0, i), code);
        }
        if (useCodeAsDefaultMessage) {
            return getMessage(code, code);
        }
        return MESSAGES.getMessage(code.replaceAll(" +", "."));
    }

    public static String getMessage(String code, String defaultMessage) {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), defaultMessage);
    }

    public static String getMessage(String code, String defaultMessage, Locale locale) {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), defaultMessage, locale);
    }

    public static String getMessage(String code, @Nullable Object[] args, String defaultMessage) {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), args, defaultMessage);
    }

    public static String getMessage(String code, @Nullable Object[] args, String defaultMessage, Locale locale) {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), args, defaultMessage, locale);
    }

    public static String getMessage(String code, Locale locale) throws NoSuchMessageException {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), locale);
    }

    public static String getMessage(String code, @Nullable Object[] args) throws NoSuchMessageException {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), args);
    }

    public static String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
        return MESSAGES.getMessage(code.replaceAll(" +", "."), args, locale);
    }

    public static String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
        return MESSAGES.getMessage(resolvable);
    }

    public static String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
        return MESSAGES.getMessage(resolvable, locale);
    }

    /**
     * 判断消息是否含有中文
     * @param message
     * @return
     */
    public static boolean isChinese(String message) {
        if (StringUtils.isEmpty(message)) {
            return false;
        }
        return ChinesePattern.matcher(message).find();
    }

}
